package com.um.pasantiaum.empresa.pedidos;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.um.pasantiaum.objetos.Pedido;
import com.um.pasantiaum.objetos.SelectedPedido;

public class OpenPedidosEmpresaFragment extends ListFragment {

	private ArrayList<Pedido> mOpenPedidos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	public class OpenPedidosAdapter extends ArrayAdapter<Pedido> {

		public OpenPedidosAdapter(ArrayList<Pedido> pedidos) {
			super(getActivity(), 0, pedidos);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						com.um.pasantiaum.R.layout.list_item_product, null);
			}

			Pedido p = getItem(position);

			TextView nameView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_name);
			nameView.setText(p.getCliente().getName());
			TextView descriptionView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_description);
			descriptionView.setText(p.getDateCreated().toString());
			TextView priceView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.product_price);
			priceView.setText(p.getPrecioTotal() + " $");

			return convertView;
		}

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Pedido p = (Pedido) ((OpenPedidosAdapter) getListAdapter())
				.getItem(position);
		Intent i = new Intent(getActivity(), DetailPedidoTruckActivity.class);
		SelectedPedido.getSelectedPedido().setPedido(p);
		startActivity(i);
	}

	@Override
	public void onResume() {
		super.onResume();
		updatePedidos();

	}

	private void updatePedidos() {
		mOpenPedidos = new ArrayList<Pedido>();

		OpenPedidosAdapter temp = ((OpenPedidosAdapter) getListAdapter());

		if (temp != null)
			temp.clear();

		ParseQuery<ParseObject> pq = ParseQuery.getQuery("Pedidos");
		pq.whereEqualTo("empresa", ParseUser.getCurrentUser());
		pq.whereEqualTo("estado", "open");
		pq.include("truck");
		pq.include("usuario");

		pq.findInBackground(new FindCallback<ParseObject>() {

			@Override
			public void done(List<ParseObject> objects, ParseException e) {
				// dlg.dismiss();
				if (e != null) {
					Toast.makeText(getActivity().getApplicationContext(),
							e.getMessage(), Toast.LENGTH_LONG).show();
				} else {
					for (ParseObject p : objects) {
						mOpenPedidos.add(new Pedido(p));
					}

					OpenPedidosAdapter adapter = new OpenPedidosAdapter(
							mOpenPedidos);
					setListAdapter(adapter);
				}

			}
		});
	}

}
